import { Command } from "commander";
import { AccountSetAsfFlags, Client, Wallet, Wallet as XrplWallet } from "xrpl";
import { Log, LogStatus, LogType } from "../util/Logger";

const program = new Command("issue-token")
    .option("-i, --issuer-seed <value>", "Issuer seed")
    .option("-c, --currency-code <value>", "Issuer currency code")
    .option("-d, --destination-seed <value>", "Destination seed")
    .option("-n, --node <value>", "The network node", "wss://sidechain-net1.devnet.rippletest.net")
    .option("-f, --faucet <value>", "The faucet to connect with", "sidechain-faucet.devnet.rippletest.net")
    .action(async ({ node, faucet, issuerSeed, currencyCode, destinationSeed }) => {
        if (node && issuerSeed && currencyCode) {
            try {
                const client = new Client(node);
                await client.connect();

                const issuerWallet = XrplWallet.fromSeed(issuerSeed);
                let destinationWallet: XrplWallet;
                if (destinationSeed) {
                    destinationWallet = XrplWallet.fromSeed(destinationSeed);
                } else {
                    Log(LogType.Tool, LogStatus.ToDo, `Creating wallet destination wallet...`);
                    destinationWallet = Wallet.generate();
                    await client.fundWallet(
                        { address: destinationWallet.address, classicAddress: destinationWallet.address } as XrplWallet,
                        {
                            faucetHost: faucet,
                        },
                    );
                    Log(
                        LogType.Tool,
                        LogStatus.Done,
                        `Wallet created with address ${destinationWallet.address} seed ${destinationWallet.seed}`,
                    );
                }

                Log(LogType.Tool, LogStatus.ToDo, `Enabling rippling from ${issuerWallet.address}...`);
                await client.submit(
                    {
                        TransactionType: "AccountSet",
                        Account: issuerWallet.address,
                        SetFlag: AccountSetAsfFlags.asfDefaultRipple,
                    },
                    {
                        autofill: true,
                        wallet: issuerWallet,
                    },
                );
                Log(LogType.Tool, LogStatus.Done, `Rippling enabled from ${issuerWallet.address}...`);

                Log(
                    LogType.Tool,
                    LogStatus.ToDo,
                    `Creating TrustLine from ${destinationWallet.address} to ${currencyCode}.${issuerWallet.address}...`,
                );
                await client.submit(
                    {
                        TransactionType: "TrustSet",
                        Account: destinationWallet.address,
                        LimitAmount: {
                            currency: currencyCode,
                            issuer: issuerWallet.address,
                            value: "100000000000000",
                        },
                    },
                    {
                        autofill: true,
                        wallet: destinationWallet,
                    },
                );
                Log(
                    LogType.Tool,
                    LogStatus.Done,
                    `TrustLine created from ${destinationWallet.address} to ${currencyCode}.${issuerWallet.address}...`,
                );
                Log(
                    LogType.Tool,
                    LogStatus.ToDo,
                    `Sending Payment from ${currencyCode}.${issuerWallet.address} to ${destinationWallet.address}...`,
                );
                await client.submit(
                    {
                        TransactionType: "Payment",
                        Account: issuerWallet.address,
                        Amount: {
                            currency: currencyCode,
                            issuer: issuerWallet.address,
                            value: "1000000",
                        },
                        Destination: destinationWallet.address,
                    },
                    {
                        autofill: true,
                        wallet: issuerWallet,
                    },
                );
                Log(LogType.Tool, LogStatus.Done, `Payment sent`);
            } catch (error) {
                console.log(error);
                process.exit(1);
            }
            process.exit(0);
        } else {
            program.help();
            process.exit(1);
        }
    });
export default program;
